package com.atguigu.dao.impl;

import com.atguigu.dao.BasicBehaviorDao;
import com.atguigu.pojo.BasicBehavior;

import java.util.List;

public class BasicBehaviorDaoImpl extends BaseDao implements BasicBehaviorDao {
    @Override
    public int addBasicBehavior(BasicBehavior basicBehavior) {
        System.out.println("BasicBehaviorDaoImpl 程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_basicBehavior(`id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type`) values(?,?,?,?,?,?,?,?,?);";
            return update(sql,basicBehavior.getId(),basicBehavior.getMainTaskId(),basicBehavior.getDeployServiceModuleId(),basicBehavior.getDeployGoalId(),basicBehavior.getCommand(),basicBehavior.getClientId(),basicBehavior.getResult(),
                    basicBehavior.getStatus(),basicBehavior.getType());
    }

    @Override
    public int deleteBasicBehaviorById(String id) {
        String sql="delete from t_basicBehavior where id=?";
        return update(sql,id);
    }

    @Override
    public int updateBasicBehavior(BasicBehavior basicBehavior) {
        String sql="update t_basicBehavior set `id`=?,`mainTaskId`=?,`deployServiceModuleId`=?,`deployGoalId`=?,`command`=?,`clientId`=?,`result`=?,`status`=?,`type`=? where id=?";
        return update(sql,basicBehavior.getId(),basicBehavior.getMainTaskId(),basicBehavior.getDeployServiceModuleId(),basicBehavior.getDeployGoalId(),
                basicBehavior.getCommand(),basicBehavior.getClientId(),basicBehavior.getResult(),basicBehavior.getStatus(),basicBehavior.getType(),
                basicBehavior.getId());
    }

    @Override
    public BasicBehavior queryBasicBehaviorById(String id) {
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type` from t_basicBehavior where id=?";
        return queryForOne(BasicBehavior.class,sql,id);
    }
    @Override
    public BasicBehavior queryBasicBehaviorByStatusMakeAndClientId(String clientId){
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type` from t_basicBehavior where status='make' and clientId=?";
        return queryForOne(BasicBehavior.class,sql,clientId);
    }
    @Override
    public BasicBehavior queryBasicBehaviorByStatusMakeAndClientIdAndMainTaskId(String clientId,String mainTaskId){
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type` from t_basicBehavior where status='make' and clientId=? and mainTaskId=?";
        return queryForOne(BasicBehavior.class,sql,clientId,mainTaskId);
    }
    @Override
    public BasicBehavior queryBasicBehaviorByStatusMakeAndClientIdAndDeployServiceModuleId(String clientId,String deployServiceModuleId){
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type` from t_basicBehavior where status='make' and clientId=? and deployServiceModuleId=?";
        return queryForOne(BasicBehavior.class,sql,clientId,deployServiceModuleId);
    }
    @Override
    public BasicBehavior queryLastBasicBehaviorByMainTaskId(String mainTaskId){
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type` from t_basicBehavior where mainTaskId=? order by id desc limit 1";
        return queryForOne(BasicBehavior.class,sql,mainTaskId);
    }
    @Override
    public BasicBehavior queryMakeStatusBasicBehaviorByDeployServiceModuleIdAndDeployGoalId(String deployServiceModuleId,String deployGoalId){
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type` from t_basicBehavior where deployServiceModuleId=? and deployGoalId=? and status='make'";
        return queryForOne(BasicBehavior.class,sql,deployServiceModuleId,deployGoalId);
    }

    @Override
    public List<BasicBehavior> queryBasicBehaviors() {
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type`  from t_basicBehavior";
        return queryForList(BasicBehavior.class,sql);
    }
    @Override
    public List<BasicBehavior> queryBasicBehaviorsByMainTaskId(String mainTaskId) {
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type`  from t_basicBehavior where mainTaskId=?";
        return queryForList(BasicBehavior.class,sql,mainTaskId);
    }
    @Override
    public List<BasicBehavior> queryBasicBehaviorsByDeployServiceModuleId(String deployServiceModuleId){
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type`  from t_basicBehavior where deployServiceModuleId=?";
        return queryForList(BasicBehavior.class,sql,deployServiceModuleId);
    }
    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_basicBehavior";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<BasicBehavior> queryForPageItems(Integer begin, Integer pageSize) {
        String sql ="select `id`,`mainTaskId`,`deployServiceModuleId`,`deployGoalId`,`command`,`clientId`,`result`,`status`,`type`  from t_basicBehavior limit ?,?";
        return queryForList(BasicBehavior.class,sql,begin,pageSize);

    }
}
